let express = require('express');
let router = express.Router();
let article = require('../services/article')
// 查询方法
router.get('/articles', (req, res, next) => {
  article.selectAll().then(
      (data) => {
        let resData = data;
        resData.msg = '查询成功'
        res.json(resData);
      }
    )
    .catch((err) => {
      let resData = [];
      resData.msg = '服务器开小差'
      res.json([resData]);
    })
});


//添加文章
router.post('/addArticle', (req, res, next) => {

  let {
    title,
    name,
    region,
    desc
  } = req.body;

  let art = {
    title,
    name,
    region,
    desc,
    createTime: new Date().toLocaleString()
  }


  console.log(art, '我是拿到的给模板的数据')
  article.save(art).then(ressult => { // 保存成功
    res.json({
      msg: '文章发布成功'
    });
  }).catch(err => { // 保存失败
    res.json({
      msg: '文章发布失败'
    });
  });
});


// 删除文章(动态路由时候用params)
router.get('/deleteArticle/:ids', (req, res, next) => {
  console.log(req.params.ids, '我是请求头的参数')
  article.removeId(req.params.ids).then(res => {
    res.json({
      msg: '文章删除成功'
    });
  }).catch(err => { // 保存失败
    res.json({
      msg: '文章删除失败'
    });
  });

});


// 添加测方法

router.post('/login', (req, res, next) => {

  console.log(req, '我是拿到的参数')
  // res.send('1111111111111')
 

  // sendMail()

  // res.render('index', {
  //   title: 'Express'
  // });
});



module.exports = router;